package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.mbs2.epics.server.entity.Album;
import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.entity.User;
import rs.mbs2.epics.server.session.dao.localinterface.AlbumDaoLocal;
import rs.mbs2.epics.server.session.dao.localinterface.PhotoDaoLocal;
import rs.mbs2.epics.server.session.dao.localinterface.UserDaoLocal;

public class ViewAlbumController extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	@EJB
	PhotoDaoLocal photoDao;
	
	@EJB
	AlbumDaoLocal albumDao;
	
	@EJB
	UserDaoLocal userDao;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String id = (String) request.getParameter("id");
		Integer albumId = Integer.parseInt(id);

		
		List<Photo> albumPhotos = photoDao.findPhotosByAlbumId(albumId);
		request.setAttribute("albumPhotos", albumPhotos);
		
		Album album = albumDao.findById(albumId);

		User albumOwner = userDao.findUserThatCreatedAlbum(album.getId());
		request.setAttribute("albumOwner", albumOwner);

		request.setAttribute("album", album);
		
		
		if(album.getTitle().equalsIgnoreCase("favorites")) 
			request.setAttribute("favorites", new Boolean(true)); 
		else 
			request.setAttribute("favorites", new Boolean(false));

		
		request.getRequestDispatcher("/viewAlbum.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
